package com.example.demo.scheduledtask;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.demo.entity.Device;
import com.example.demo.mapper.DeviceMapper;
import com.example.demo.utils.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

/**
 * 定时同步设备状态
 * 只同步编号AMA开头的设备状态
 *
 */
@Component
@Slf4j
public class DeviceStopStatusScheduledTask {
//
//    @Autowired
//    private DeviceMapper deviceMapper;
//
//    @Autowired
//    private RestTemplateUtil restTemplateUtil;
//
//
//    @Scheduled(cron = "0/30 * * * * ?")
//    @Async(value = "taskExecutor")
//    public void deviceStopStatusScheduledTask(){
//        ////log.info("定时同步设备设备状态");
//        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.likeRight(Device::getDeviceno,"AMA")
//                .isNotNull(Device::getDptno);
//        List<Device> devices = deviceMapper.selectList(queryWrapper);
//        //log.info(devices.toString());
//        if(CollectionUtils.isNotEmpty(devices)){
//            for (Device device : devices) {
//                Map<String, Object> params = new HashMap<>();
//                params.put("deviceId",device.getDeviceno());
//                try {
//                    JSONObject jsonObject = restTemplateUtil.get("http://172.19.255.89:8083/zwz/deviceDataNew/getDeviceStatusByDevicedIdOnScreen", params);
//                    //log.info(jsonObject.toJSONString());
//                    String result = jsonObject.get("result").toString();
//                    if("connected".equals(result)){
//                        if(device.getRunstates() == null || device.getRunstates() != 0){
//                            device.setRunstates(0);
//                            LambdaUpdateWrapper<Device> updateWrapper = new LambdaUpdateWrapper<>();
//                            updateWrapper.eq(Device::getDeviceno, device.getDeviceno());
//                            deviceMapper.update(device,updateWrapper);
//                        }
//
//                    }else {
//                        if(device.getRunstates() == null || device.getRunstates() != 1){
//                            device.setRunstates(1);
//                            LambdaUpdateWrapper<Device> updateWrapper = new LambdaUpdateWrapper<>();
//                            updateWrapper.eq(Device::getDeviceno, device.getDeviceno());
//                            deviceMapper.update(device,updateWrapper);
//                        }
//                    }
//
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }
//    }
}
